package com.example.saveword.save_word;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.saveword.R;
import com.example.saveword.dao.StudyRecordDao;
import com.example.saveword.model.User;
import com.example.saveword.utils.JsonUtils;
import com.example.saveword.utils.SPUtils;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CalendarActivity extends AppCompatActivity implements View.OnClickListener{
    private ImageView btn_backToMain;
    private CalendarView calendarView;
    private TextView tv_todayLearn, tv_todayReview;
    private int userId;
    private User user;
    private StudyRecordDao studyRecordDao;
    List<String> checked = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);
        init();
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.btn_backToMain) { // 返回来时的界面
            finish();
        }
    }

    private void init() {
        studyRecordDao = new StudyRecordDao(this);
        user = (User) JsonUtils.jsonToObject((String) SPUtils.get(this, "User", ""), User.class);
        userId = user.get_id();
        btn_backToMain = findViewById(R.id.btn_backToMain);
        calendarView = findViewById(R.id.calendarView);
        tv_todayLearn = findViewById(R.id.tv_todayLearn);
        tv_todayReview = findViewById(R.id.tv_todayReview);

        btn_backToMain.setOnClickListener(this);

        // 日历相关的设定
        calendarView.setOnCalendarSelectListener(new com.haibin.calendarview.CalendarView.OnCalendarSelectListener() {

            @Override
            public void onCalendarOutOfRange(Calendar calendar) {

            }

            @Override
            public void onCalendarSelect(Calendar calendar, boolean isClick) {
                int year = calendar.getYear();
                int month = calendar.getMonth();
                int day = calendar.getDay();
                Date now = new Date(year - 1900, month - 1, day);
                SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");
                String date = ft.format(now);
                String studiedWords = studyRecordDao.getStudiedByDay(String.valueOf(userId), date);
                String repeatedWords = studyRecordDao.getRepeatedByDay(String.valueOf(userId), date);
                tv_todayLearn.setText(studiedWords);
                tv_todayReview.setText(repeatedWords);
            }
        });
        // 找到所有打卡的日期
        checked = studyRecordDao.getAllChecked(String.valueOf(userId));
        Map<String, Calendar> map = new HashMap<>();
        for(int i = 0; i < checked.size(); i ++) {
            String str = checked.get(i);
            String []date = str.split("-");
            int y = Integer.parseInt(date[0]);
            int m = Integer.parseInt(date[1]);
            int d = Integer.parseInt(date[2]);
            map.put(getSchemeCalendar(y, m, d, 0xFF40db25, "").toString(),
                    getSchemeCalendar(y, m, d, 0xFF40db25, ""));
        }
        calendarView.setSchemeDate(map);
    }

    private Calendar getSchemeCalendar(int year, int month, int day, int color, String text) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(color); // 如果单独标记颜色，则会使用这个颜色
        calendar.setScheme(text);
        return calendar;
    }
}